<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-07-05 15:28:43
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-07-06 09:28:56
 * @Description: 
 * 
-->
<template>
	<el-radio-group v-model="value" size="small">
		<el-radio-button
			v-for="item in options"
			:key="item[props.value]"
			:label="item[props.value]"
			>{{ item[props.label] }}</el-radio-button
		>
	</el-radio-group>
</template>

<script>
import config from "@/config/select";

export default {
	props: {
		valueEqKey: { type: Boolean, default: false },
		apiObj: { type: Object, default: () => {} },
		dic: { type: String, default: "" },
		params: { type: Object, default: () => ({}) },
		modelValue: {},
	},
	emits: ["valueChange", "update:modelValue"],
	data() {
		return {
			loading: false,
			options: [],
			props: config.props,
			value:
				this.modelValue == null ? null : (this.modelValue ?? "") + "",
			change: {},
		};
	},
	mounted() {
		//如果有默认值就去请求接口获取options
		if (this.dic || this.apiObj) this.getRemoteData();
	},

	watch: {
		modelValue(val) {
			this.value = val;
		},
		value() {
			this.$emit("valueChange", this.value);
			this.$emit("update:modelValue", this.value);
		},
	},
	methods: {
		//获取数据
		async getRemoteData() {
			this.loading = true;
			var res = {};
			if (this.apiObj) {
				res = await this.apiObj.get(this.params);
			} else if (this.dic) {
				res = await config.dicApiObj.get(this.params);
			}
			var response = config.parseData(res);

			this.options = (response.data.rows ?? response.data)?.map((v) => {
				v.key = (v.key ?? v.id) + "";
				return v;
			});

			this.loading = false;
		},
	},
};
</script>
